{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Shape error gallery\n\nGallery of contours with shape error\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\nfrom numpy import arange, cos, linspace, radians, sin\n\nfrom py_eddy_tracker import data\nfrom py_eddy_tracker.dataset.grid import RegularGridDataset\nfrom py_eddy_tracker.eddy_feature import Contours\nfrom py_eddy_tracker.generic import local_to_coordinates"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Method to built circle from center coordinates\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def build_circle(x0, y0, r):\n    angle = radians(linspace(0, 360, 50))\n    x_norm, y_norm = cos(angle), sin(angle)\n    return local_to_coordinates(x_norm * r, y_norm * r, x0, y0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We iterate over closed contours and sort with regards of shape error\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "g = RegularGridDataset(\n    data.get_demo_path(\"dt_med_allsat_phy_l4_20160515_20190101.nc\"),\n    \"longitude\",\n    \"latitude\",\n)\nc = Contours(g.x_c, g.y_c, g.grid(\"adt\") * 100, arange(-50, 50, 0.2))\ncontours = dict()\nfor coll in c.iter():\n    for current_contour in coll.get_paths():\n        _, _, _, aerr = current_contour.fit_circle()\n        i = int(aerr // 4) + 1\n        if i not in contours:\n            contours[i] = list()\n        contours[i].append(current_contour)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Shape error gallery\nFor each contour display, we display circle fitted, we work at different latitude circle could have distorsion\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=(12, 12))\nfor i in range(1, 26):\n    e_min, e_max = (i - 1) * 4, i * 4\n    ax = plt.subplot(5, 5, i, title=f\" {e_min} < err < {e_max}\")\n    ax.xaxis.set_ticklabels([])\n    ax.yaxis.set_ticklabels([])\n    ax.set_aspect(\"equal\")\n    ax.grid()\n    if i in contours:\n        for contour in contours[i]:\n            x, y = contour.lon, contour.lat\n            x0, y0, radius, _ = contour.fit_circle()\n            if x.shape[0] > 30 and 30000 < radius < 70000:\n                # Plot only first contour found\n                m = ax.plot(x, y, \"r\")[0]\n                ax.plot(*build_circle(x0, y0, radius), \"g--\")\n                ax.plot(x0, y0, \"k.\")\n                break\nplt.tight_layout()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}